const mongoose = require('mongoose');
const theatersModel = mongoose.model('theaters');

let dataExist = async () => {
    let data = await theatersModel.find();
    if (!data.length) {
        require('../mock/theaters')
    }
}
dataExist();
module.exports = {
    get: async ({ curPage, eachPage } = {}) => {
        eachPage = ~~eachPage;
        let result = {};
        result.total = await theatersModel.countDocuments();
        result.maxPage = Math.ceil(result.total / eachPage);
        result.rows = await theatersModel.find()
            .skip((curPage - 1) * eachPage)
            .limit(eachPage)
            .populate('cinemasId')
            .exec();

        return result;
    },
    add: async (obj = {}) =>
        await theatersModel.create(obj),
    getOne: async ({ _id } = {}) =>
        await theatersModel.findOne({ _id }).populate('cinemasId').exec(),
    upDate: async (conditions, doc) =>
        await theatersModel.updateOne(conditions, doc),
    delete: async ({ _id }) =>
        await theatersModel.deleteOne({ _id }),
    getBycinemasId: async (_id) => {
        const data = await theatersModel.find({ cinemasId: _id }).exec();
        return data;
    },
    search: async ({ _id }) =>
        await theatersModel.find({ cinemasId: _id }).populate('cinemasId').exec(),
}